#-*- coding: utf-8 -*-
from openerp.osv import osv, fields
import logging
_logger = logging.getLogger(__name__)

class open_academy_session(osv.osv):
    _name="open.academy.session"
    _description="just for practice"
    _open_academy_states = [('draft', 'Draft'), ('standby', 'Standby'), ('ready', 'Ready'), ('started', 'Started'), ('closed', 'Closed')]
    _columns = {
        'name': fields.char(u"名称", size=64, select=True),
        'instructor_id': fields.many2one('res.partner', u'教师', domain=[('is_teacher', '=', True)], context={'is_teacher': 1}, required=1),
        'course_id': fields.many2one('open.academy.course', u'班级'),
        'seat': fields.integer(u'座位'),
        'start_date': fields.date(u'开始时间', required=True),
        'duration': fields.float(u'周期', digits=(16, 2)),
        'attendee_ids': fields.many2many('res.partner', 'session_partner_rel', 'session_id', 'partner_id', u'学生', readonly=True),
        'state': fields.selection(_open_academy_states, 'state', readonly=True),
    }

    def open_academy_standby(self, cr, uid, ids, context=None):
        self.wirte(cr, uid, ids[0], {'state': 'standby'})

    def open_academy_draft(self, cr, uid, ids, context=None):
        self.wirte(cr, uid, ids[0], {'state': 'draft'})

    def open_academy_ready(self, cr, uid, ids, context=None):
        self.wirte(cr, uid, ids[0], {'state': 'ready'})

    def open_academy_started(self, cr, uid, ids, context=None):
        self.wirte(cr, uid, ids[0], {'state': 'started'})

    def open_academy_closed(self, cr, uid, ids, context=None):
        self.wirte(cr, uid, ids[0], {'state': 'closed'})

    _defaults = {
        'state': lambda *a: 'draft'
    }

class open_academy_course(osv.osv):
    _name="open.academy.course"
    _description=u"课程"
    _columns = {
        'name': fields.char(u'课程名', size=64, select=True),
        'responsible_id': fields.many2one('res.users', u'负责人'),
        'session_ids': fields.one2many('open.academy.session', 'course_id', u"名称"),
    }

class open_academy_teacher(osv.osv):
    _inherit = 'res.partner'
    _columns = {
        'is_teacher': fields.boolean('teacher'),
    }
    _defaults = {
        'is_teacher': lambda s, c, uid, context: context.get('is_teacher', False) and True
    }

class open_academy_add_attendee(osv.osv_memory):
    _name = 'open.academy.add.attendee'
    _description = 'add_attendee'
    _columns = {
        'session_id' : fields.many2one('open.academy.session', u'课程计划'),
        'attendee_ids': fields.one2many('open.academy.attendees', 'attendee_id', u'学生'),
    }

    def add_attendee_2_session(self, cr, uid, ids, context=None):

        obj_session = self.pool.get('open.academy.session')

        #取得用户当前输入值, 放在add_attendee这个变量里
        add_attendee = self.browse(cr, uid, ids[0], context=context)
        #将报名的学生写入课程计划
        for x in add_attendee.attendee_ids:
            obj_session.write(cr, uid, add_attendee.session_id.id, {'attendee_ids': [(4, x.partner_id.id)]}, context)

class open_academy_attendees(osv.osv_memory):
    _name='open.academy.attendees'
    _description='attendees'
    _columns = {
        'attendee_id': fields.many2one('open.academy.add.attendee', u'添加'),
        'partner_id': fields.many2one('res.partner', u'学生'),
    }
#   _rec_name = 'partner_id'
